// Copyrights by Kenneth Lee, 2022. All Rights Reserved
#include "float_val.hpp"

FloatVal::FloatVal(int i) {
	v = (float)i;
	state = ST_VAL;
}

FloatVal::FloatVal() {
	state = ST_NON;
}

FloatVal::FloatVal(const char *s) {
	*this = FloatVal(atoi(s));
}

bool operator==(const FloatVal &l, const FloatVal &r) {
	if (!((ValIf)l).state_compare((ValIf &)r))
		return false;

	return l.v == r.v;
}

ostream & operator<<(ostream & os, const FloatVal & v) {
	if (v.state != ValIf::ST_VAL)
		((ValIf)v).show(os);
	else 
		os << v.v;

	return os;
}

FloatVal operator+(const FloatVal &l, const FloatVal &r) {
	FloatVal ret;
	if (((ValIf&)ret).op_init(l, r))
		ret.v = l.v + r.v;
	return ret;
}

FloatVal operator-(const FloatVal &l, const FloatVal &r) {
	FloatVal ret;
	if (((ValIf&)ret).op_init(l, r))
		ret.v = l.v - r.v;
	return ret;
}

FloatVal operator*(const FloatVal &l, const FloatVal &r) {
	FloatVal ret;
	if (((ValIf&)ret).op_init(l, r))
		ret.v = l.v * r.v;
	return ret;
}

FloatVal operator/(const FloatVal &l, const FloatVal &r) {
	FloatVal ret;
	if (((ValIf&)ret).op_init(l, r)) {
		if (r.v != 0)
			ret.v = l.v / r.v;
		else {
			ret.state = ValIf::ST_INV;
		}
	}
	return ret;
}

FloatVal & FloatVal::operator=(const char *s) {
	*this = FloatVal(atoi(s));
	return *this;
}
